package com.qf.controller;

import com.alibaba.druid.util.StringUtils;
import com.qf.pojo.User;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    //注册
    @RequestMapping("/register")
    public String register(HttpServletRequest req){
        //获取参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String gender = req.getParameter("gender");

        //设置对象属性
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setGender(gender);

        //调用方法
        boolean isSuccess = userService.register(user);

        //响应前端
        //判断注册成功和失败的结果
        if (isSuccess){
            //返回登陆页面
            return "redirect:/login";
        }else {
            //返回注册页面
            return "redirect:/register";
        }
    }

    //检验用户名是否存在
    @RequestMapping("/checkUserName")
    public void checkUserName(HttpServletRequest  req, HttpServletResponse resp) throws IOException {
        //获取参数
        String username = req.getParameter("username");
        //输出
        PrintWriter writer = resp.getWriter();

        if (StringUtils.isEmpty(username)){
            writer.write("0");
        }else {
            User user = userService.findByUsername(username);

            if (user == null){
                //username在数据库中不存在，返回2
                writer.write("2");
            }else {
                //存在，返回1
                writer.write("1");
            }
        }
    }

    //登录
    @RequestMapping("/login")
    public String login(HttpServletRequest  req, HttpServletResponse resp) throws UnsupportedEncodingException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //判断是否自动登录
        String auto = req.getParameter("auto");

        User user = userService.findByUserAndPwd(username,password);

        if (user == null) {
            //重定向
            return "redirect:/login";
        }else {
            //保存登录状态
            req.getSession().setAttribute("user",user);
            //保存登录状态到cookie中
            if ("on".equalsIgnoreCase(auto)){
                String encode = URLEncoder.encode(username + "#" + password, "utf-8");
                //创建cookie对象
                Cookie cookie = new Cookie("user",encode);

                //设置域名
                cookie.setDomain("localhost");
                //设置路径
                cookie.setPath("/");
                //设置周期
                cookie.setMaxAge(14 * 24 * 60 *60);
                //响应cookie
                resp.addCookie(cookie);
            }
        }
        return "redirect:/index";
    }

    //检验验证码
//    @RequestMapping("/checkCode")
//    public void checkCode(HttpServletRequest  req, HttpServletResponse resp) throws IOException {
//        String inputCode = req.getParameter("code");
//        //输出
//        PrintWriter writer = resp.getWriter();
//        //从session中获取验证码
//        Object codeObj = req.getSession().getAttribute("code");
//
//        if (codeObj == null){
//            writer.write("1");
//        }else {
//            String code = (String) codeObj;
//            if (code.equalsIgnoreCase(inputCode)){
//                writer.write("0");
//            }else {
//                writer.write("1");
//            }
//        }
//    }

    //退出登录
    @RequestMapping("/logOut")
    public String logOut(HttpServletRequest  req){
        //清空session
        req.getSession().invalidate();

        return "redirect:/login";
    }
}
